package com.ews.onlineexchange.controller;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ManagerUser;
import com.ews.onlineexchange.service.EntityQuery;
import org.apache.commons.lang3.StringUtils;

@Model
public class SystemUserList extends EntityQuery<ManagerUser> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	

	private ManagerUser managerUser = new ManagerUser();
	
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<ManagerUser> entity) {
		List<Predicate> restrictions = super.getRestrictions(cb, entity);
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		restrictions.add(cb.like(entity.get("cnname"), "%" + getStringRestriction(getManagerUser().getCnname()) + "%"));
		restrictions.add(cb.like(entity.get("username"), "%" + getStringRestriction(getManagerUser().getUsername()) + "%"));
		String region = getManagerUser().getRegion();
		if(StringUtils.isNotBlank(region))restrictions.add(cb.equal(entity.get("region"), getManagerUser().getRegion()));
		return restrictions;
	}

	public SystemUserList() {
		setOrder("");
		setMaxResults(10);
	}

	public ManagerUser getManagerUser() {
		return managerUser;
	}

}
